--[[
Project: HunterRP
File: doors.lua
Author: Sebihunter
]]--

local tableDoors = "doors_"..game.GetMap()

function initDoors()
	local tExst = 1
	if (!sql.TableExists(tableDoors)) then
		tExst = 0
		query = "CREATE TABLE "..tableDoors.." ( unique_id int, ownable int, owner varchar(255), name varchar(255), price int, locked int )"
		result = sql.Query(query)
		if (sql.TableExists(tableDoors)) then
			Msg("Succes ! table 1 created \n")
		else
			Msg("Somthing went wrong with the "..tableDoors.." query ! \n")
			Msg( sql.LastError( result ) .. "\n" )
		end
	end
	
	timer.Create("loadHouses", 1, 1, function()		
	for k, v in pairs(ents.GetAll()) do	
		local class = v:GetClass()
		if (class == "func_door" or class == "func_door_rotating" or class == "prop_door_rotating") then
			if tExst == 0 then
				sql.Query( "INSERT INTO "..tableDoors.." (`unique_id`, `ownable`, `owner`, `name`, `price`, `locked`)VALUES ('"..k.."', '1', 'nobody', 'none', '100', '0')" )
				v:SetNWInt("unique_id", tonumber(k))
				v:SetNWInt("ownable", 1)
				v:SetNWString("owner", "nobody")
				v:SetNWString("name", "none")
				v:SetNWInt("price", 100)
				v:SetNWInt("locked", 0)
			else
				local unique_id = sql.QueryValue("SELECT unique_id FROM "..tableDoors.." WHERE unique_id = '"..k.."'")
				local ownable = sql.QueryValue("SELECT ownable FROM "..tableDoors.." WHERE unique_id = '"..k.."'")
				local owner = sql.QueryValue("SELECT owner FROM "..tableDoors.." WHERE unique_id = '"..k.."'")
				local name = sql.QueryValue("SELECT name FROM "..tableDoors.." WHERE unique_id = '"..k.."'")
				local price = sql.QueryValue("SELECT price FROM "..tableDoors.." WHERE unique_id = '"..k.."'")
				local locked = sql.QueryValue("SELECT locked FROM "..tableDoors.." WHERE unique_id = '"..k.."'")
				v:SetNWInt("unique_id", tonumber(unique_id))
				v:SetNWInt("ownable", ownable)
				v:SetNWString("owner", owner)
				v:SetNWString("name", name)
				v:SetNWInt("price", price)		
				v:SetNWInt("locked", locked)
			end
		end
	end
	
	timer.Create("saveHouses", 60, 0, function()	
		for j, i in pairs(ents.GetAll()) do	
			local class = i:GetClass()
			if (class == "func_door" or class == "func_door_rotating" or class == "prop_door_rotating") then
				local unique_id = i:GetNWInt("unique_id")
				local ownable = i:GetNWInt("ownable")
				local owner = i:GetNWString ("owner")
				local name = i:GetNWString ("name")
				local price = i:GetNWInt("price")
				--unique_id = math.floor(unique_id*0.1)

				--Make that when the server restarts all buyable appartments and shops are aviable again
				if tonumber(ownable) == 1 then	
					sql.Query("UPDATE "..tableDoors.." SET ownable ="..ownable.." , owner = 'nobody', name = '"..name.."', price = "..price.." WHERE unique_id = '"..unique_id.."'")	
				else
					sql.Query("UPDATE "..tableDoors.." SET ownable ="..ownable.." , owner = '"..owner.."', name = '"..name.."', price = "..price.." WHERE unique_id = '"..unique_id.."'")	
				end
			end
		end
	end)
	
	end)
end

function GM:ShowTeam( ply ) -- Buying/Selling a door | This hook is called everytime F2 is pressed.
	local trace = ply:GetEyeTrace()
	if ValidEntity(trace.Entity) and trace.Entity:GetPos():Distance(ply:GetPos()) < 400 then
		if trace.Entity:IsDoor() then
			if tonumber(trace.Entity:GetNWInt("ownable")) == 1 then
				if trace.Entity:GetNWString ("owner") == "nobody" then
					if tonumber(trace.Entity:GetNWInt ("price")) <= tonumber(ply:GetNWInt ("money")) then
						ply:PrintMessage( HUD_PRINTTALK, "Door owned! You paid "..tostring(trace.Entity:GetNWInt ("price")).."$!" )
						trace.Entity:SetNWString ("owner", ply:GetNWString("name"))
						ply:SetNWInt ("money",tonumber((ply:GetNWInt("money"))-(trace.Entity:GetNWInt ("price"))))
					else
						ply:PrintMessage( HUD_PRINTTALK, "You do not have enough money to buy this door!" )
					end
				elseif trace.Entity:GetNWString ("owner") == ply:GetNWString("name") then
					trace.Entity:SetNWString ("owner", "nobody")
					ply:SetNWInt ("money",tonumber((ply:GetNWInt("money")+(math.floor(trace.Entity:GetNWInt ("price")*0.75)))))
					ply:PrintMessage( HUD_PRINTTALK, "Door sold! You got "..tostring(tonumber(math.floor(trace.Entity:GetNWInt ("price")*0.75))).."$!" )
				end
			end				
		end
	end    
end